package com.xxx.yuedong.mall.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.xxx.yuedong.common.R;
import com.xxx.yuedong.mall.model.vo.CartVO;
import com.xxx.yuedong.mall.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 描述：     购物车Controller
 */

@RestController
@RequestMapping("/cart")
@Api(tags = "前台购物车相关接口")
@Slf4j
public class CartController {

    @Autowired
    CartService cartService;

    @GetMapping("/list")
    @ApiOperation(value = "购物车列表")
    public R list() {
        //内部获取用户ID，防止横向越权
        List<CartVO> cartList = cartService.list(StpUtil.getLoginIdAsInt());
        return R.ok().put("list",cartList);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加商品到购物车")
    public R add(@RequestParam Integer productId, @RequestParam Integer count) {
        List<CartVO> cartVOList = cartService.add(StpUtil.getLoginIdAsInt(), productId, count);
        return R.ok().put("list",cartVOList);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新购物车")
    public R update(@RequestParam Integer productId, @RequestParam Integer count) {
        List<CartVO> cartVOList = cartService
                .update(StpUtil.getLoginIdAsInt(), productId, count);
        return R.ok().put("list",cartVOList);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除购物车")
    public R delete(@RequestParam Integer productId) {
        //不能传入userID，cartID，否则可以删除别人的购物车
        List<CartVO> cartVOList = cartService.delete(StpUtil.getLoginIdAsInt(), productId);
        return R.ok().put("list",cartVOList);
    }

    @PostMapping("/select")
    @ApiOperation(value = "选择/不选择购物车的某商品")
    public R select(@RequestParam Integer productId, @RequestParam Integer selected) {
        //不能传入userID，cartID，否则可以删除别人的购物车
        List<CartVO> cartVOList = cartService
                .selectOrNot(StpUtil.getLoginIdAsInt(), productId, selected);
        return R.ok().put("list",cartVOList);
    }

    @PostMapping("/selectAll")
    @ApiOperation(value = "全选择/全不选择购物车的某商品")
    public R selectAll(@RequestParam Integer selected) {
        //不能传入userID，cartID，否则可以删除别人的购物车
        List<CartVO> cartVOList = cartService
                .selectAllOrNot(StpUtil.getLoginIdAsInt(), selected);
        return R.ok().put("list",cartVOList);
    }
}
